package com.google.code.timetrail.gui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.CardLayout;

import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.AbstractListModel;
import javax.swing.UIManager;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.AncestorListener;
import javax.swing.event.AncestorEvent;

import com.google.code.timetrail.backend.*;
import com.google.code.timetrail.backend.travelSpeed;
import com.google.code.timetrail.backend.consumptionRate;
public class GameFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JLabel gameTitleLabel;
	private JLabel enterNameLabel;
	private JTextField enterNameTextField;
	private JLabel addMembersLabel;
	private JButton addButton;
	private JList professionList;
	private JLabel professionTextField;
	private JList additionalMembersList;
	private JButton nextButton;
	private JLabel rationsLabel;
	private JLabel paceLabel;
	private JList rationsList;
	private JList paceList;
	private JButton nextButton2;
	private JButton backButton;
	private JFrame frame;
	private DefaultListModel model;
	protected int addButtonCount;
	private List additionalPlayers;
	private String selectedProfession;
	private travelSpeed pace;
	private consumptionRate rations;
	private Control gameControl;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {

		try{
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch(Exception e){
			System.err.println("Unable to use native look and feel");
		}

		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GameFrame frame = new GameFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public GameFrame() {
		gameControl = new Control();
		additionalPlayers = new ArrayList<String>();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 600, 500);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new CardLayout(0, 0));
		
		JPanel startupPanel = new JPanel();
		contentPane.add(startupPanel, "name_1317839433132283000");
		startupPanel.setLayout(null);
		
		gameTitleLabel = new JLabel("Time Trail\n");
		gameTitleLabel.setBounds(295, 0, 67, 32);
		startupPanel.add(gameTitleLabel);
		
		enterNameLabel = new JLabel("Enter Name:");
		enterNameLabel.setBounds(200, 66, 76, 16);
		startupPanel.add(enterNameLabel);
		
		enterNameTextField = new JTextField();
		enterNameTextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				
			}
		});
		enterNameTextField.setBounds(295, 60, 134, 28);
		enterNameTextField.setText("player1");
		startupPanel.add(enterNameTextField);
		enterNameTextField.setColumns(10);
		
		addMembersLabel = new JLabel("Enter Name of additional Party members:");
		addMembersLabel.setBounds(19, 101, 257, 16);
		startupPanel.add(addMembersLabel);
		
		final DefaultListModel model = new DefaultListModel();
		additionalMembersList = new JList(model);
		additionalMembersList.setBounds(295, 133, 134, 115);
		additionalMembersList.setVisibleRowCount(4);
		startupPanel.add(additionalMembersList);
		
		addButton = new JButton("Add");
		addButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(addButtonCount<4){
				String s = (String)JOptionPane.showInputDialog(
				                    frame,
				                    "Enter Name of additional Party member",
				                    "Name"
				                    );
				
				if ((s != null) && (s.length() > 0)) {
					int pos = additionalMembersList.getModel().getSize();
					model.add(pos,s);
					additionalPlayers.add(s);
					addButtonCount++;
				    return;
				}
				}

				//If you're here, the return value was null/empty.
				//setLabel("Come on, finish the sentence!");
			}
		});
		addButton.setBounds(295, 100, 75, 29);
		startupPanel.add(addButton);
		
		professionList = new JList();
		professionList.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				if(professionList.getSelectedIndex() == 0){
					selectedProfession = "Engineer";
				} else if(professionList.getSelectedIndex() == 1){
					selectedProfession = "Nutritionist";
				} else if(professionList.getSelectedIndex() == 2){
					selectedProfession = "Doctor";
				} else if(professionList.getSelectedIndex() == 3){
					selectedProfession = "Entrepreneur";
				}
			}
		});
		professionList.setModel(new AbstractListModel() {
			String[] values = new String[] {"Engineer", "Nutritionist", "Doctor", "Entrepreneur"};
			public int getSize() {
				return values.length;
			}
			public Object getElementAt(int index) {
				return values[index];
			}
		});
		professionList.setBounds(295, 260, 134, 76);
		professionList.setVisibleRowCount(4);
		startupPanel.add(professionList);
		
		professionTextField = new JLabel("Choose Profession:");
		professionTextField.setBounds(155, 260, 121, 16);
		startupPanel.add(professionTextField);
		
		nextButton = new JButton("Next >");
		nextButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				CardLayout cd = (CardLayout) contentPane.getLayout();
                
                String name = enterNameTextField.getText();
                
                Person player = null;
                if(name.equals("") || name == null || professionList.getSelectedIndex() == -1){
                	JOptionPane.showMessageDialog(frame,
                		    "Please fill in all required information.",
                		    "Some fields were left empty",
                		    JOptionPane.PLAIN_MESSAGE);
                } else {
                	if(selectedProfession.equals("Engineer")){
                		player = new Engineer();
                	} else if(selectedProfession.equals("Nutritionist")){
                		player = new Nutritionist();
                	} else if(selectedProfession.equals("Doctor")){
                		player = new Doctor();
                	} else if(selectedProfession.equals("Entrepreneur")){
                		player = new Entrepreneur();
                	}
                	ArrayList<Person> members = new ArrayList<Person>();
	                	for(int i = 0; i < additionalPlayers.size(); i++){
	                		members.add(new Person((String) additionalPlayers.get(i)));
	                	}
                	
                	player.setName(name);
                	gameControl.setPlayer(player);
                	gameControl.setMembers(members);
                	cd.next(contentPane);
                	//selectedProfession - the String selected profession
                	//name = player name, can't be empty or null
                	//additionalPlayers - array that holds the additional players, null if empty
                	
                }
			}
		});
		nextButton.setBounds(447, 412, 117, 29);
		startupPanel.add(nextButton);
		
		JPanel paceAndRationsPanel = new JPanel();
		contentPane.add(paceAndRationsPanel, "name_1317840867491837000");
		paceAndRationsPanel.setLayout(null);
		
		paceLabel = new JLabel("Select Pace:");
		paceLabel.setBounds(164, 131, 73, 16);
		paceAndRationsPanel.add(paceLabel);
		
		rationsLabel = new JLabel("Select Rations:");
		rationsLabel.setBounds(139, 285, 98, 16);
		paceAndRationsPanel.add(rationsLabel);
		
		rationsList = new JList();
		rationsList.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				if(rationsList.getSelectedIndex() == 0){
					rations = consumptionRate.NONE;
				} else if(rationsList.getSelectedIndex() == 1){
					rations = consumptionRate.BAREBONES;
				} else if(rationsList.getSelectedIndex() == 2){
					rations = consumptionRate.MEAGER;
				} else if(rationsList.getSelectedIndex() == 3){
					rations = consumptionRate.NORMAL;
				} else if(rationsList.getSelectedIndex() == 4){
					rations = consumptionRate.WELLFED;
				}
			}
		});
		rationsList.setModel(new AbstractListModel() {
			String[] values = new String[] {"None ", "bare-bones ", "Meager ", "Normal", "Wellfed "};
			public int getSize() {
				return values.length;
			}
			public Object getElementAt(int index) {
				return values[index];
			}
		});
		rationsList.setBounds(250, 285, 118, 85);
		paceAndRationsPanel.add(rationsList);
		
		paceList = new JList();
		paceList.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent arg0) {
				if(paceList.getSelectedIndex() == 0){
					pace = travelSpeed.STOPPED;
				} else if(paceList.getSelectedIndex() == 1){
					pace = travelSpeed.MACH;
				} else if(paceList.getSelectedIndex() == 2){
					pace = travelSpeed.WARP;
				} else if(paceList.getSelectedIndex() == 3){
					pace = travelSpeed.LUDICROUS;
				}
			}
		});
		paceList.setModel(new AbstractListModel() {
			String[] values = new String[] {"Stopped", "Mach", "Warp", "Ludacris"};
			public int getSize() {
				return values.length;
			}
			public Object getElementAt(int index) {
				return values[index];
			}
		});
		paceList.setBounds(250, 131, 118, 68);
		paceAndRationsPanel.add(paceList);
		
		nextButton2 = new JButton("Next >");
		nextButton2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(paceList.getSelectedIndex() == -1 || rationsList.getSelectedIndex() == -1){
					JOptionPane.showMessageDialog(frame,
                		    "Please fill in all required information.",
                		    "Some fields were left empty",
                		    JOptionPane.PLAIN_MESSAGE);
				} else {
					
					
					gameControl.setPace(pace);
					gameControl.setRations(rations);
					JOptionPane.showMessageDialog(frame,
                		    "Configuration Complete",
                		    "Configuration Complete",
                		    JOptionPane.PLAIN_MESSAGE);
				}
			}
		});
		nextButton2.setBounds(447, 412, 117, 29);
		paceAndRationsPanel.add(nextButton2);
		
		backButton = new JButton("< Back");
		backButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				CardLayout cd = (CardLayout) contentPane.getLayout();
                cd.previous(contentPane);
			}
		});
		backButton.setBounds(320, 412, 117, 29);
		paceAndRationsPanel.add(backButton);
	}
}
